package com.maybach.core.ao.impl;

import com.maybach.common.model.CarFormalitiesDO;
import com.maybach.common.query.CarFormalitiesQuery;
import com.maybach.core.ao.CarFormalitiesAO;
import com.maybach.core.form.carFormalities.CarFormalitiesForm;
import com.maybach.core.manager.CarFormalitiesManager;
import com.maybach.util.CalendarUtil;
import com.maybach.util.CollectionUtil;
import com.maybach.util.ImgUrlUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.velocity.tools.generic.DateTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by weishuai on 16/3/1.
 */
@Component
public class CarFormalitiesAOImpl implements CarFormalitiesAO {
    Logger logger = LoggerFactory.getLogger(this.getClass());


    @Autowired
    CarFormalitiesManager carFormalitiesManager;

    @Override
    public void updateCarFormalities(CarFormalitiesForm carFormalitiesForm) {

        try {
            CarFormalitiesDO carFormalitiesDO = carFormalitiesManager.selectByPrimaryKey(carFormalitiesForm.getId());
            if (carFormalitiesDO == null) {
                return;
            }
            CarFormalitiesDO c = getCarFormalitiesDO(carFormalitiesForm);
            carFormalitiesManager.updateByPrimaryKeySelective(c);
        } catch (Exception e) {
            logger.error("error in updateCarFormalities,params:" + carFormalitiesForm.toString());
        }

    }

    private CarFormalitiesDO getCarFormalitiesDO(CarFormalitiesForm carFormalitiesForm) {
        CarFormalitiesDO c = null;
        try {
            c = new CarFormalitiesDO();
            c.setId(carFormalitiesForm.getId());
            c.setFormalitiesImgs(ImgUrlUtil.mergeList(carFormalitiesForm.getFormalitiesImgs()));
            c.setLogisticsImgs(ImgUrlUtil.mergeList(carFormalitiesForm.getLogisticsImgs()));
            c.setReceiveCompany(carFormalitiesForm.getReceiveCompany());
            c.setReceiveNo(carFormalitiesForm.getReceiveNo());
            c.setReceiveTime(CalendarUtil.toDate(carFormalitiesForm.getReceiveTime(), CalendarUtil.TIME_PATTERN));
            c.setSendCompany(carFormalitiesForm.getSendCompany());
            c.setSendNo(carFormalitiesForm.getSendNo());
            c.setSendTime(CalendarUtil.toDate(carFormalitiesForm.getSendTime(), CalendarUtil.TIME_PATTERN));
            c.setId(carFormalitiesForm.getId());
            c.setOutOrderId(carFormalitiesForm.getOutOrderId());
        } catch (Exception e) {
            logger.error("error in getCarFormalitiesDO,params:" + carFormalitiesForm.toString());
        }
        return c;
    }

    @Override
    public Long addCarFormalities(CarFormalitiesForm carFormalitiesForm) {
        try {
            CarFormalitiesDO c = getCarFormalitiesDO(carFormalitiesForm);
            return carFormalitiesManager.insertSelective(c);
        } catch (Exception e) {
            logger.error("error in addCarFormalities,params:"+carFormalitiesForm.toString());
        }
        return null;

    }

    @Override
    public CarFormalitiesForm getById(Long id) {
        CarFormalitiesForm carFormalitiesForm = new CarFormalitiesForm();
        try {
            CarFormalitiesDO c = carFormalitiesManager.selectByPrimaryKey(id);
            if(c!=null){
                carFormalitiesForm.setId(c.getId());
                carFormalitiesForm.setSendNo(c.getSendNo());
                carFormalitiesForm.setSendCompany(c.getSendCompany());
                carFormalitiesForm.setFormalitiesImgs(ImgUrlUtil.parseList(c.getFormalitiesImgs()));
                carFormalitiesForm.setLogisticsImgs(ImgUrlUtil.parseList(c.getLogisticsImgs()));
                carFormalitiesForm.setReceiveCompany(c.getReceiveCompany());
                carFormalitiesForm.setReceiveNo(c.getReceiveNo());
                carFormalitiesForm.setReceiveTime(CalendarUtil.toString(c.getReceiveTime(),CalendarUtil.TIME_PATTERN));
                carFormalitiesForm.setSendTime(CalendarUtil.toString(c.getSendTime(),CalendarUtil.TIME_PATTERN));
            }
        } catch (Exception e) {
            logger.error("error in getById,params:id:" + id);
        }
        return carFormalitiesForm;
    }

    @Override
    public CarFormalitiesForm getByOutOrderId(Long outOrderId) {
        CarFormalitiesForm carFormalitiesForm = null;
        try {
            carFormalitiesForm = new CarFormalitiesForm();
            CarFormalitiesQuery query=new CarFormalitiesQuery();
            query.createCriteria().andOutOrderIdEqualTo(outOrderId);
            List<CarFormalitiesDO> ls= carFormalitiesManager.selectByQuery(query);
            CarFormalitiesDO c=null;
            if(CollectionUtils.isNotEmpty(ls)){
                c=ls.get(0);
            }
            if(c!=null){
                carFormalitiesForm.setId(c.getId());
                carFormalitiesForm.setSendNo(c.getSendNo());
                carFormalitiesForm.setSendCompany(c.getSendCompany());
                carFormalitiesForm.setFormalitiesImgs(ImgUrlUtil.parseList(c.getFormalitiesImgs()));
                carFormalitiesForm.setLogisticsImgs(ImgUrlUtil.parseList(c.getLogisticsImgs()));
                carFormalitiesForm.setReceiveCompany(c.getReceiveCompany());
                carFormalitiesForm.setReceiveNo(c.getReceiveNo());
                carFormalitiesForm.setReceiveTime(CalendarUtil.toString(c.getReceiveTime(),CalendarUtil.TIME_PATTERN));
                carFormalitiesForm.setSendTime(CalendarUtil.toString(c.getSendTime(),CalendarUtil.TIME_PATTERN));
            }
        } catch (Exception e) {
            logger.error("error in getByOutOrderId,outOrderId:"+outOrderId);
        }
        return carFormalitiesForm;
    }
}
